<!DOCTYPE html>
<html>
<head>
{include file='top.tpl'}
</head>
<body class="skin-blue">
<div class="wrapper">
  <div>
    {include file="bread_crumbs.tpl"}
    <!--引入面包屑-->
    <section class="web-setting-wrap">
      <div class="row">
        <div class="col-md-12">
            <div class="box-header">
              <p class="box-title-sm no-margin">
                <span><b>角色：</b>{$editInfo.role_name}</span>
                <span><b>描述：</b>{$editInfo.description}</span>
              </p>
            </div>
            <form>
            <div class="box-body">
              <table class="table table-bordered table-hover role-auth-table">
                <thead>
                  <tr>
                    <th width="30%">
                      <span class="checkbox pull-left"><label><input type="checkbox" id="js_checkAllCb">全选菜单</label></span>
                      <span class="text-center">菜单权限</span>
                    </th>
                    <th>页面功能元素</th>
                  </tr>
                </thead>
                <tbody>
                {foreach from=$menus key=topMenuKey item=topMenu}
                  <tr class="warning tr_{$topMenu.menu_id}">
                    <td class="text-left">
                      <span class="checkbox"><label><input type="checkbox" name='role_auth[]' id='{$topMenu.menu_id}' class='{$topMenu.menu_id}' {if isset($menuPrivilege[$topMenu.menu_id]) && $menuPrivilege[$topMenu.menu_id] == 1}checked{/if}>{$topMenu.name}</label></span>
                    </td>
                    <td>&nbsp;</td>
                  </tr>
                  <!--一级-->
                  {foreach from=$topMenu.children key=secondMenuKey item=secondMenu}
                  <tr class="info tr_{$topMenu.menu_id}">
                    <td class="text-left" style="padding-left:50px;">
                      <span class="checkbox"><label><input type="checkbox" name='role_auth[]' id='{$secondMenu.menu_id}' parent_id='{$secondMenu.fid}' class='{$topMenu.menu_id}' {if isset($menuPrivilege[$secondMenu.menu_id]) && $menuPrivilege[$secondMenu.menu_id] == 1}checked{/if}>{$secondMenu.name}</label></span>
                    </td>
                    <td>&nbsp;</td>
                  </tr>
                  <!--二级-->
                  {foreach from=$secondMenu.children key=thirdMenuKey item=thirdMenu}
                  <tr class='tr_{$topMenu.menu_id}'>
                    <td class="text-left" style="padding-left:100px;">
                      <span class="checkbox"><label><input type="checkbox" name='role_auth[]' id='{$thirdMenu.menu_id}' parent_id='{$thirdMenu.fid}' class='{$topMenu.menu_id}' {if isset($menuPrivilege[$thirdMenu.menu_id]) && $menuPrivilege[$thirdMenu.menu_id] == 1}checked{/if}>{$thirdMenu.name}</label></span>
                    </td>
                    <td class="text-left">
                    {foreach from=$thirdMenu.children key=fourthMenuKey item=fourthMenu}
                        <span class="checkbox"><label><input type="checkbox" name='role_auth[]' id='{$fourthMenu.menu_id}' parent_id='{$fourthMenu.fid}' class='{$topMenu.menu_id}' {if isset($menuPrivilege[$fourthMenu.menu_id]) && $menuPrivilege[$fourthMenu.menu_id] == 1}checked{/if}>{$fourthMenu.name}</label></span>
                    {/foreach}
                     </td>
                  </tr>
                  {/foreach}
                  {/foreach}
                  {/foreach}

                </tbody>
              </table>
            </div>
            <div class="box-footer text-center">                    
                <input type='hidden' name='updateId' value='{$updateId}' id='updateId'/>
                <button type="button" class="btn btn-primary btn-sm" id='save_privilege'>确定</button>
                <a id="back" class="btn btn-default btn-sm" href="javascript:;" onclick="history.back();">返回</a>
            </div>
          </form>
        </div>
      </div>
    </section>
  </div>
</div>
{include file='foot.tpl'}
{literal}
<script>
  $(function(){
    $("#js_checkAllCb").on("change",function(){
      var $this = $(this),
          thisParent = $this.parents("table.role-auth-table");
      if($this.prop("checked")==true){
        thisParent.find("input[type='checkbox']").prop("checked",true);
      }
      else{
        thisParent.find("input[type='checkbox']").prop("checked",false);
      }
    });
    
    $("input[name='role_auth[]']").on("click",function(){
    	check_all_down($(this));
    	check_all_up($(this));
    });
    
  });
  
  function check_all_down(obj){
	  if(obj.is(':checked'))
      {
		  var to_stats = true;
      }else{
    	  var to_stats = false;
      }
	  clas = obj.attr('class');
	  $('tr.tr_'+clas+'').each(function (){
		  $(this).find("input[name='role_auth[]']").each(function(){
				 if($(this).attr('parent_id') == obj.attr('id')){
					 $(this).prop("checked",to_stats);
					 check_all_down($(this));
				 }
		  });
	  });
  }
  
  function check_all_up(obj){
	  if(obj.is(':checked'))
      {
		  var to_stats = true;
		  clas = obj.attr('class');
		  $('tr.tr_'+clas+'').each(function (){
			  $(this).find("input[name='role_auth[]']").each(function(){
					 if($(this).attr('id') == obj.attr('parent_id')){
						 $(this).prop("checked",to_stats);
						 check_all_up($(this));
					 }
			  });
		  });
      }else{
    	  //var to_stats = false;
      }
  }
  
  $("#save_privilege").click(function(){
	  updateId = $('#updateId').val();
	  privileges = new Array();
	  $('input[name="role_auth[]"]:checked').each(function(){
		  privileges.push($(this).attr('id'));
	  })
	  if(updateId>0)
	  {
		  $.ajax({
	          type:"POST",
	          url:"/system/role/setprivilege",
	          data:{'privileges':privileges,'updateId':updateId},
	          dataType: "json",
	          success:function(res){
                if (res.status == 0) {
                    layer.alert(res.content);
                    return false;
                };
	            layer.alert(res.content);
	            location.reload();
	          }
	      });
	   }else{
		   layer.alert('异常'); 
	   }
  });
  
</script>
{/literal}
</body>
</html>
